package com.dingwei.superiordiary.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.VarietyShopAdapter;
import com.dingwei.superiordiary.bean.TabFragmentTitleItem;
import com.dingwei.superiordiary.bean.VerietyShopBean;
import com.dingwei.superiordiary.mvp.presenter.VarietyShopPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.dingwei.superiordiary.ui.activity.NewSuperiorProductsActivity;
import com.dingwei.superiordiary.ui.activity.RankingListActivity;
import com.dingwei.superiordiary.ui.activity.SearchEditActivity;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.LazyLoadFragment;
import com.lihao.baselibrary.utils.ScreenUtils;
import com.lihao.baselibrary.weight.TitleBar;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lihao on 2018/1/4.
 * 首页-杂货铺
 */

public class VarietyShopFragment extends LazyLoadFragment implements Contract.VarietyShopView {
    VarietyShopPresenter varietyShopPresenter = new VarietyShopPresenter(this);
    @Bind(R.id.fake_status_bar)
    View fakeStatusBar;
    @Bind(R.id.title_bar)
    TitleBar titleBar;
    @Bind(R.id.img_ranking_list)
    ImageView imgRankingList;
    @Bind(R.id.img_new_superior)
    ImageView imgNewSuperior;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.data_layout)
    NestedScrollView dataLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;


    private VarietyShopAdapter mAdapter;

    @Override
    protected int setContentView() {
        return R.layout.fragment_variety_shop;
    }

    @Override
    protected void setBindView() {
        ButterKnife.bind(this, view);
        initView();
    }
    /**
     * 设置状态栏高度 主要是设配刘海屏手机
     */
    private void setStatusBarHeight() {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ScreenUtils.getStatusHeight(BaseActivity.context));
        fakeStatusBar.setLayoutParams(lp);
    }
    private void initView() {

        setStatusBarHeight();

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mAdapter = new VarietyShopAdapter(BaseActivity.context);
        mRecyclerView.setAdapter(mAdapter);


        mAdapter.xxInterfaceListener(new VarietyShopAdapter.xxInterFace() {
            @Override
            public void xx(int position) {
                List<TabFragmentTitleItem> list = new ArrayList<>();
                for (int i = 0; i < mAdapter.getAllData().size(); i++) {
                    TabFragmentTitleItem item = new TabFragmentTitleItem();
                    item.title = mAdapter.getAllData().get(i).name;
                    item.id = mAdapter.getAllData().get(i).id;
                    list.add(item);
                }
                intent = new Intent(BaseActivity.context, RankingListActivity.class);
                intent.putExtra("activity_title", "货铺");
                intent.putExtra("selectPosition", position);
                intent.putExtra("fragmentTitles", (Serializable) list);
                startActivity(intent);
            }
        });

        titleBar.setOnMenuListener(new TitleBar.OnMenuListener() {
            @Override
            public void onMenuClick() {
                intent = new Intent(BaseActivity.context, SearchEditActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void lazyLoad() {
        varietyShopPresenter.initData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.img_ranking_list, R.id.img_new_superior, R.id.no_net_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_ranking_list:
                intent = new Intent(BaseActivity.context, RankingListActivity.class);
                startActivity(intent);
                break;
            case R.id.img_new_superior:
                intent = new Intent(BaseActivity.context, NewSuperiorProductsActivity.class);
                intent.putExtra("activity_title", "新优品");
                intent.putExtra("order", "2");
                startActivity(intent);
                break;
            case R.id.no_net_btn:
                varietyShopPresenter.initData();
                break;
        }
    }

    @Override
    public void setData(VerietyShopBean bean) {
        mAdapter.clear();
        mAdapter.addAll(bean.data);
        showData("success");
    }

    @Override
    public void onFail() {

        showData("fail");
    }

    public void showData(String state) {
        if (state.equals("success")) {
            dataLayout.setVisibility(View.VISIBLE);
            noNetLayout.setVisibility(View.GONE);
        } else {
            dataLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }
}
